package kn.cstf2.cop.Client;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.logging.Level;
import kn.cstf2.cop.Bean.*;
import kn.cstf2.cop.Shared.CopShared;
import kn.cstf2.cop.Server.CopServer;
import kn.cstf2.global.Utils.ConexionFtp;
import kn.cstf2.global.Utils.CstF2Exception;
import kn.cstf2.implementation.Bean.ClienteStkBean;
import kn.cstf2.implementation.Bean.T_asignStkBean;
import kn.cstf2.implementation.Bean.TransitionsBeanImp;
import kn.cstf2.main.Shared.MainShared;
import kn.cstf2.transition.Bean.TransitionBean;
import kn.cstf2.transition.Client.TransitionClient;
import kn.cstf2.transition.Shared.TransitionShared;
import kn.mx.cstf2.dto.UsuarioDto;

import org.apache.log4j.Logger;

public class CopActionImportDefault extends ActionSupport {

    private static Logger log = Logger.getLogger(TransitionClient.class);
    public static final String ERROR_SESSION = "errorSession";
    public static final String ERROR_KNKEY = "errorKnkey";
    public String ZIP = "zip";
    public String EXPORT = "export";
    private Boolean exportar;
    private String exporta;
    private TransitionBean transitionBeanM = new TransitionBean();
    private TransitionBean transitionBean = new TransitionBean();
    private Boolean gralVobo;
    private Boolean gralVoboSop;
    private ClienteStkBean financialBI = new ClienteStkBean();
    private String s1;
    private String delete;
    //private String fileNameUpload;
    private File fileNameUpload;
    ContactsBean contactsBean = null;
    private ArrayList<String> contactsList = new ArrayList<String>();
    private String requierementsTxt;
    private CopBean subproducts = new CopBean();
    private String originConditionsTxt;
    private String destinationConditionsTxt;
    private String workingInstructionsTxt;
    private String fileListOnServerSelected;
    private InputStream fileInputStream;
    private ArrayList<String> fileListOnServer = new ArrayList<String>();
    private ArrayList<String> creditTermsList = new ArrayList<String>();
    private ArrayList<String> billingCurrencyList = new ArrayList<String>();
    private ArrayList<String> invoiceRevDate = new ArrayList<String>();
    private ArrayList<String> invoiceReceDate = new ArrayList<String>();
    private ArrayList<String> fileAttachList = new ArrayList<String>();
    private ClienteStkBean cp = new ClienteStkBean();
    private ExceptionBean exceptionBean = new ExceptionBean();
    private KpiBean kpiBean = new KpiBean();
    private ArrayList<ContactsBean> detallesContactsList;
    private ArrayList<KpiBean> detalleSpecialList = new ArrayList<KpiBean>();
    private ArrayList<ExceptionBean> detallesExceptionList;
    private T_customer_solutionBeanSTK t_customer_solutionBeanSTK = new T_customer_solutionBeanSTK();
    private CopBean copBeanJSP = new CopBean();
    private String submitFormMethod = new String();
    private String overviewhidden = new String();
    private String nombreArchivo = "";
    private String attach = "attachments";
    private int index = 0;

    public String getNombreArchivo() {
        return nombreArchivo;
    }

    public void setNombreArchivo(String nombreArchivo) {
        this.nombreArchivo = nombreArchivo;
    }

    public String getOverviewhidden() {
        return overviewhidden;
    }

    public void setOverviewhidden(String overviewhidden) {
        this.overviewhidden = overviewhidden;
    }

    public String getSubmitFormMethod() {
        return submitFormMethod;
    }

    public void setSubmitFormMethod(String submitFormMethod) {
        this.submitFormMethod = submitFormMethod;
    }

    public Boolean getExportar() {
        return exportar;
    }

    public void setExportar(Boolean exportar) {
        this.exportar = exportar;
    }

    public CopBean getCopBeanJSP() {
        return copBeanJSP;
    }

    public void setCopBeanJSP(CopBean copBeanJSP) {
        this.copBeanJSP = copBeanJSP;
    }

    public T_customer_solutionBeanSTK getT_customer_solutionBeanSTK() {
        return t_customer_solutionBeanSTK;
    }

    public void setT_customer_solutionBeanSTK(T_customer_solutionBeanSTK t_customer_solutionBeanSTK) {
        this.t_customer_solutionBeanSTK = t_customer_solutionBeanSTK;
    }

    public KpiBean getKpiBean() {
        return kpiBean;
    }

    public void setKpiBean(KpiBean kpiBean) {
        this.kpiBean = kpiBean;
    }

    public ExceptionBean getExceptionBean() {
        return exceptionBean;
    }

    public void setExceptionBean(ExceptionBean exceptionBean) {
        this.exceptionBean = exceptionBean;
    }

    public ArrayList<ExceptionBean> getDetallesExceptionList() {
        return detallesExceptionList;
    }

    public void setDetallesExceptionList(ArrayList<ExceptionBean> detallesExceptionList) {
        this.detallesExceptionList = detallesExceptionList;
    }

    public ArrayList<KpiBean> getDetalleSpecialList() {
        return detalleSpecialList;
    }

    public void setDetalleSpecialList(ArrayList<KpiBean> detalleSpecialList) {
        this.detalleSpecialList = detalleSpecialList;
    }

    public ArrayList<String> getFileAttachList() {
        return fileAttachList;
    }

    public void setFileAttachList(ArrayList<String> fileAttachList) {
        this.fileAttachList = fileAttachList;
    }

    public String getS1() {
        return s1;
    }

    public void setS1(String s1) {
        this.s1 = s1;
    }

    public String getDelete() {
        return delete;
    }

    public void setDelete(String delete) {
        this.delete = delete;
    }

    public File getFileNameUpload() {
        return fileNameUpload;
    }

    public void setFileNameUpload(File fileNameUpload) {
        this.fileNameUpload = fileNameUpload;
    }

    public ClienteStkBean getFinancialBI() {
        return financialBI;
    }

    public void setFinancialBI(ClienteStkBean financialBI) {
        this.financialBI = financialBI;
    }

    public ArrayList<ContactsBean> getDetallesContactsList() {
        return detallesContactsList;
    }

    public void setDetallesContactsList(ArrayList<ContactsBean> detallesContactsList) {
        this.detallesContactsList = detallesContactsList;
    }

    public ClienteStkBean getCp() {
        return cp;
    }

    public void setCp(ClienteStkBean cp) {
        this.cp = cp;
    }

    public InputStream getFileInputStream() {
        return fileInputStream;
    }

    public void setFileInputStream(InputStream fileInputStream) {
        this.fileInputStream = fileInputStream;
    }

    public String getFileListOnServerSelected() {
        return fileListOnServerSelected;
    }

    public void setFileListOnServerSelected(String fileListOnServerSelected) {
        this.fileListOnServerSelected = fileListOnServerSelected;
    }

    public ArrayList<String> getBillingCurrencyList() {
        return billingCurrencyList;
    }

    public void setBillingCurrencyList(ArrayList<String> billingCurrencyList) {
        this.billingCurrencyList = billingCurrencyList;
    }

    public ArrayList<String> getCreditTermsList() {
        return creditTermsList;
    }

    public void setCreditTermsList(ArrayList<String> creditTermsList) {
        this.creditTermsList = creditTermsList;
    }

    public ArrayList<String> getInvoiceReceDate() {
        return invoiceReceDate;
    }

    public void setInvoiceReceDate(ArrayList<String> invoiceReceDate) {
        this.invoiceReceDate = invoiceReceDate;
    }

    public ArrayList<String> getInvoiceRevDate() {
        return invoiceRevDate;
    }

    public void setInvoiceRevDate(ArrayList<String> invoiceRevDate) {
        this.invoiceRevDate = invoiceRevDate;
    }

    public ArrayList<String> getFileListOnServer() {
        return fileListOnServer;
    }

    public void setFileListOnServer(ArrayList<String> fileListOnServer) {
        this.fileListOnServer = fileListOnServer;
    }

    public String getWorkingInstructionsTxt() {
        return workingInstructionsTxt;
    }

    public void setWorkingInstructionsTxt(String workingInstructionsTxt) {
        this.workingInstructionsTxt = workingInstructionsTxt;
    }

    public String getDestinationConditionsTxt() {
        return destinationConditionsTxt;
    }

    public void setDestinationConditionsTxt(String destinationConditionsTxt) {
        this.destinationConditionsTxt = destinationConditionsTxt;
    }

    public String getOriginConditionsTxt() {
        return originConditionsTxt;
    }

    public void setOriginConditionsTxt(String originConditionsTxt) {
        this.originConditionsTxt = originConditionsTxt;
    }

    public CopBean getSubproducts() {
        return subproducts;
    }

    public void setSubproducts(CopBean subproducts) {
        this.subproducts = subproducts;
    }

    public String getRequierementsTxt() {
        return requierementsTxt;
    }

    public void setRequierementsTxt(String requierementsTxt) {
        this.requierementsTxt = requierementsTxt;
    }

    public ArrayList<String> getContactsList() {
        return contactsList;
    }

    public void setContactsList(ArrayList<String> contactsList) {
        this.contactsList = contactsList;
    }

    public ContactsBean getContactsBean() {
        return contactsBean;
    }

    public void setContactsBean(ContactsBean contactsBean) {
        this.contactsBean = contactsBean;
    }

    public String getExporta() {
        return exporta;
    }

    public void setExporta(String exporta) {
        this.exporta = exporta;
    }

    public TransitionBean getTransitionBean() {
        return transitionBean;
    }

    public void setTransitionBean(TransitionBean transitionBean) {
        this.transitionBean = transitionBean;
    }

    public TransitionBean getTransitionBeanM() {
        return transitionBeanM;
    }

    public void setTransitionBeanM(TransitionBean transitionBeanM) {
        this.transitionBeanM = transitionBeanM;
    }
    private ArrayList<TransitionBean> transitionBeanArray;

    public ArrayList<TransitionBean> getTransitionBeanArray() {
        return transitionBeanArray;
    }

    public void setTransitionBeanArray(ArrayList<TransitionBean> transitionBeanArray) {
        this.transitionBeanArray = transitionBeanArray;
    }

    public String execute() {
        UsuarioDto usuario = new UsuarioDto();
        ActionContext contexto = ActionContext.getContext();
        Map<String, Object> sesion = contexto.getSession();
        usuario = (UsuarioDto) sesion.get("usuarioSession");
        Map<String, Object> sesion2 = contexto.getSession();
        transitionBeanM = (TransitionBean) sesion2.get("Knkey");

        if (transitionBeanM.getKnkey() == null) {
            CstF2Exception ex = new CstF2Exception();
            log.error("ERROR. ", ex);
            addFieldError("Not Knkey Found", "PorFavor Seleccione una Knkey: " + ex.getMessage());
            MainShared mainShared = new MainShared();
            setTransitionBeanArray(mainShared.GetTransitionBeanArrayIdKnkey(usuario));

            return ERROR_KNKEY;
        }

        String sbm = getSubmitFormMethod();
        if (sbm.equals("SaveInfoUser05")) {
            SaveInfoUser05();
        }
        try {
            contactsList.add("SU");
            contactsList.add("RU");
            contactsList.add("Guest");

            CopShared copShared = new CopShared();
            ClienteStkBean clienteStkBean = new ClienteStkBean();
            TransitionShared transitionShared = new TransitionShared();

            cp = copShared.GetAllDataFromClientesSTK(transitionBeanM.getKnkey());
            clienteStkBean = copShared.GetAcountVisibilitySTK(transitionBeanM.getKnkey());
            cp.setAccounts_visibility(clienteStkBean.getAccounts_visibility());

            t_customer_solutionBeanSTK = copShared.GetCustomerSolutionStk(transitionBeanM.getKnkey());

            financialBI = copShared.GetAllDataFromClientesSTK(transitionBeanM.getKnkey());

            TransitionBean transitionBeanNew = new TransitionBean();

            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            subproducts = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);

            detalleSpecialList = copShared.GetKpiSTK(subproducts.getId());
            detallesExceptionList = copShared.GetExceptionListSTK(subproducts.getId());

            copBeanJSP = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);

            ConexionFtp conexionFtp = new ConexionFtp(attach);
            fileListOnServer = conexionFtp.enlistaDir(transitionBeanM.getKnkey());
            
            detallesContactsList = copShared.getContactList(subproducts.getId());

        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String actualizarpagina(Boolean exportar) {

        try {
            contactsList.add("SU");
            contactsList.add("RU");
            contactsList.add("Guest");

            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            CopShared copShared = new CopShared();
            ClienteStkBean clienteStkBean = new ClienteStkBean();
            TransitionShared transitionShared = new TransitionShared();

            cp = copShared.GetAllDataFromClientesSTK(transitionBeanM.getKnkey());
            clienteStkBean = copShared.GetAcountVisibilitySTK(transitionBeanM.getKnkey());
            cp.setAccounts_visibility(clienteStkBean.getAccounts_visibility());

            t_customer_solutionBeanSTK = copShared.GetCustomerSolutionStk(transitionBeanM.getKnkey());

            financialBI = copShared.GetAllDataFromClientesSTK(transitionBeanM.getKnkey());

            TransitionBean transitionBeanNew = new TransitionBean();

            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            subproducts = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);

            detalleSpecialList = copShared.GetKpiSTK(subproducts.getId());
            detallesExceptionList = copShared.GetExceptionListSTK(subproducts.getId());

            copBeanJSP = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);

            ConexionFtp conexionFtp = new ConexionFtp(attach);
            fileListOnServer = conexionFtp.enlistaDir(transitionBeanM.getKnkey());
            
            detallesContactsList = copShared.getContactList(copBeanJSP.getId());

        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String GetAllDataFroSTK() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            CopShared copShared = new CopShared();
            ClienteStkBean clienteStkBean = new ClienteStkBean();

            clienteStkBean = copShared.GetAllDataFromClientesSTK(transitionBeanM.getKnkey());

            actualizarpagina(exportar);
        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String ExportCop() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            CopBean copBean = new CopBean();
            copBean = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);
            if (copBean.getId() == null) {
                copShared.InsertDefaultValuesEnCop(true, transitionBeanM.getId());
            }

            actualizarpagina(exportar);
        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String removeContact() {
        UsuarioDto usuario = new UsuarioDto();
        ActionContext contexto = ActionContext.getContext();
        Map<String, Object> sesion = contexto.getSession();
        usuario = (UsuarioDto) sesion.get("usuarioSession");
        Map<String, Object> sesion2 = contexto.getSession();
        transitionBeanM = (TransitionBean) sesion2.get("Knkey");

        contactsList.add("SU");
        contactsList.add("GUEST");
        contactsList.add("RU");

        TransitionShared transitionShared = new TransitionShared();
        CopShared copShared = new CopShared();
        CopBean copBean = new CopBean();

        TransitionBean transitionBeanNew = new TransitionBean();
        transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
        transitionBeanM = transitionBeanNew;

        copBean = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);

        detallesContactsList = copShared.getContactList(copBean.getId());
        copShared.removeContacto(detallesContactsList.get(getIndex()).getId());
        Map<String, Object> sesionListP = contexto.getSession();
        sesionListP.put("ListaContacts", detallesContactsList);
        
        actualizarpagina(exportar);
        
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String AssingContactC03False() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            contactsList.add("SU");
            contactsList.add("GUEST");
            contactsList.add("RU");

            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();
            CopBean copBean = new CopBean();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            copBean = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);
            contactsBean.setIdcop(copBean.getId());

            copShared.InsertIntoContats(contactsBean);  //lo q hay desde el jsp

            Map<String, Object> sesionListP = contexto.getSession();
            detallesContactsList = (ArrayList<ContactsBean>) sesionListP.get("ListaContacts");

            if (detallesContactsList == null) {
                detallesContactsList = new ArrayList<ContactsBean>();

                detallesContactsList.add(contactsBean);

            } else {
                detallesContactsList.add(contactsBean);
            }
            sesionListP.put("ListaContacts", detallesContactsList);

            transitionBeanM.setTime(copShared.GetTheDateTime());
            transitionBeanM.setContacts(true);

            TransitionsBeanImp tbdate2 = new TransitionsBeanImp();
            Date date = new Date();
            DateFormat dateFormatCQL = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
            String fecha = dateFormatCQL.format(date);

            transitionBeanM.setContactsDate(fecha);

            copShared.UpdateContactsyContacsDateIntoTransitions(transitionBeanM);

            actualizarpagina(exportar);
        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String DeletePersonAsContac04tFalse() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");


            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();
            CopBean copBean = new CopBean();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            copBean = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);

            ContactsBean contactsBeanId = new ContactsBean();
            contactsBeanId = copShared.GetDatosDeContacts(copBean.getId());

            copShared.DeleteContacto(contactsBeanId.getIdcop());

            copShared.ContarContactosEnTransitions(transitionBeanM.getId());

            actualizarpagina(exportar);
        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String SaveInfoUser05() {

        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();

            ClienteStkBean clienteStkBean = new ClienteStkBean();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            TransitionsBeanCop transitionsBeanCop = new TransitionsBeanCop();

            String sbp = subproducts.getOverview();

            if (sbp.length() > 4) {
                transitionsBeanCop.setReporting(true);
                transitionsBeanCop.setId(transitionBeanM.getId());
                Date date = new Date();
                DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
                String fecha = dateFormat.format(date);
                transitionsBeanCop.setReportingdate(fecha);

                copShared.UpdateReportingTransition(transitionsBeanCop);
            }

            if (subproducts.getFcl() == true || subproducts.getLcl() == true || subproducts.getPerishables() == true || subproducts.getBrokerage() == true || subproducts.getIntermodal() == true || subproducts.getInsurance() == true) {
                transitionsBeanCop.setScopeofservice(true);
                transitionsBeanCop.setId(transitionBeanM.getId());
                Date datesos = new Date();
                DateFormat dateFormatsos = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
                String fechasos = dateFormatsos.format(datesos);
                transitionsBeanCop.setScopeofservicedate(fechasos);

                copShared.UpdateScopeOfServiceTransition(transitionsBeanCop);
            }

            String oc = subproducts.getOriginconditions();
            String dc = subproducts.getDestconditions();
            if (oc.length() > 4 || dc.length() > 4) {
                transitionsBeanCop.setCondition(true);
                transitionsBeanCop.setId(transitionBeanM.getId());
                Date dateOrc = new Date();
                DateFormat dateFormatOrc = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
                String fechaOrc = dateFormatOrc.format(dateOrc);
                transitionsBeanCop.setConditionsdate(fechaOrc);

                copShared.UpdateConditionTransition(transitionsBeanCop);
            }

            String wi = subproducts.getWorkinst();
            if (wi.length() > 4) {
                transitionsBeanCop.setWorkingst(true);
                transitionsBeanCop.setId(transitionBeanM.getId());
                Date dateWrk = new Date();
                DateFormat dateFormatWrk = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
                String fechaWrk = dateFormatWrk.format(dateWrk);
                transitionsBeanCop.setWorkingstdate(fechaWrk);

                copShared.UpdateWorkingSTTransition(transitionsBeanCop);
            }

            //este es de otro modulo lo tengo q guardar en sesion para que lo valide aki
//         if(gralVoboSop==true){
//            transitionsBeanCop.setSop(true);
//            transitionsBeanCop.setId(transitionBeanM.getId());
//            
//            copShared.UpdateWorkingSTTransition(transitionsBeanCop);
//        }

            if (subproducts.getPickuptosiling() == true || subproducts.getCustomsclearance() == true || subproducts.getMaincarriage() == true || subproducts.getArrivaltocustoms() == true || subproducts.getArrivaltodelivery()) {
                transitionsBeanCop.setKpi(true);
                transitionsBeanCop.setId(transitionBeanM.getId());
                Date dateKpi = new Date();
                DateFormat dateFormatKpi = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
                String fechaKpi = dateFormatKpi.format(dateKpi);
                transitionsBeanCop.setKpidate(fechaKpi);

                copShared.UpdateKpiTransition(transitionsBeanCop);
            }

            if (financialBI != null) {
                transitionsBeanCop.setBilling(true);
                transitionsBeanCop.setId(transitionBeanM.getId());
                Date dateBil = new Date();
                DateFormat dateFormatBil = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
                String fechaBil = dateFormatBil.format(dateBil);
                transitionsBeanCop.setBillingdate(fechaBil);

                copShared.UpdateBillingTransition(transitionsBeanCop);
            }
            //tambien aka sakar de sesion el valor del otro modulo
//         if(gralVobo ==true){
//            transitionsBeanCop.setCop(true);
//            transitionsBeanCop.setId(transitionBeanM.getId());
//            
//            copShared.UpdateContactsTransition(transitionsBeanCop);
//         }         

            if (detallesContactsList != null) {
                transitionsBeanCop.setContacts(true);
                transitionsBeanCop.setId(transitionBeanM.getId());
                Date dateCon = new Date();
                DateFormat dateFormatCon = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
                String fechaCon = dateFormatCon.format(dateCon);
                transitionsBeanCop.setContactsdate(fechaCon);

                copShared.UpdateContactsTransition(transitionsBeanCop);
            }


            //traer primero los datos de cop para despues hacerlees update sin perder los datos enviados desde el jsp
            //valido si tienes propiedades nulas
            CopBean copBean = new CopBean();
            copBean = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);
            subproducts = copShared.validarsihaynull(subproducts, copBean);
            copShared.UpdateCop(subproducts);

            transitionsBeanCop = copShared.SelectAllDataFromTransition(transitionBeanM.getId());
            copShared.UpdateTransition(transitionsBeanCop);

            actualizarpagina(exportar);
        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

//    public String UploadtxtLogAlert06(){
//      try{
//        UsuarioDto usuario = new UsuarioDto();
//        ActionContext contexto = ActionContext.getContext();
//        Map<String, Object> sesion = contexto.getSession();
//        usuario=(UsuarioDto)sesion.get("usuarioSession");
//        Map<String, Object> sesion2 = contexto.getSession();
//        transitionBeanM=(TransitionBean) sesion2.get("Knkey");
//        
//        TransitionShared transitionShared = new TransitionShared();
//        CopShared copShared = new CopShared();
//        CopBean copBean = new CopBean();
//        ClienteStkBean clienteStkBean = new ClienteStkBean();
//        
//        TransitionBean transitionBeanNew=new TransitionBean();
//        transitionBeanNew=transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
//        transitionBeanM = transitionBeanNew;
//   
//        String nombree="";
//        for(int i=0; i< getNombreArchivo().length(); i++){
//            if(String.valueOf( getNombreArchivo().charAt(i)).equalsIgnoreCase(".")){
//                nombree+="_"+transitionBeanNew.getKnkey()+String.valueOf( getNombreArchivo().charAt(i));
//            }else{
//            nombree+=String.valueOf(getNombreArchivo().charAt(i));
//            }
//        }
//   
// 
//        ConexionFtp conexionFtp = new ConexionFtp();
//        File name=getFileNameUpload(); 
// 
//       
//        nombree = name.getParent()+"\\"+nombree;
//        String nombrepath="";
//        
//        for(int i=0; i< nombree.length(); i++){
//            if(i==nombree.length()-1){
//            }else{
//            nombrepath+=String.valueOf(nombree.charAt(i));
//            }
//        }
//       
//        setS1("c:\\consultas.rar");
//        //File firstLocalFile = new File("c:/consultas.rar");
//        File firstLocalFile = new File(name.getParent());
//       
//        
//       
// 
//            
//            String nombre = firstLocalFile.getName();
//            FileInputStream inputStream = new FileInputStream(firstLocalFile);
//            //FileInputStream inputStream = new FileInputStream(firstLocalFile);
//    
//            System.out.println("Start uploading first file");
//            boolean done = conexionFtp.guarda(nombre, inputStream);
//            
//            fileListOnServer = conexionFtp.enlistaDir();
//            
//            inputStream.close();
//            if (done) {
//                System.out.println("The first file is uploaded successfully.");
//            }
//         actualizarpagina(exportar);
//        }catch (CstF2Exception ex) {
//            log.error("ERROR. ", ex);
//            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
//            return ERROR_SESSION;
//        }
//        catch (Exception ex) {
//            log.error("ERROR. ", ex);
//            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
//            return ERROR_SESSION;
//        }
//        if(exportar==false){
//        return SUCCESS;}else{return EXPORT;}
//    }
    public String downloadFileAction() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();
            CopBean copBean = new CopBean();
            ClienteStkBean clienteStkBean = new ClienteStkBean();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            ConexionFtp conexionFtp = new ConexionFtp(attach);
            fileInputStream = conexionFtp.obtenInfo(getFileListOnServerSelected());
            fileListOnServer = conexionFtp.getNombresArchivos();


        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        } catch (Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        return ZIP;
    }

    public String deleteFileAction() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();
            CopBean copBean = new CopBean();
            ClienteStkBean clienteStkBean = new ClienteStkBean();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;


            ConexionFtp conexionFtp = new ConexionFtp(attach);

            conexionFtp.borra(getFileListOnServerSelected(), "attachments");

            fileListOnServer = conexionFtp.enlistaDir(transitionBeanM.getKnkey());
            actualizarpagina(exportar);
        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        } catch (Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String AssingExceptionFalse() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();
            CopBean copBean = new CopBean();
            ClienteStkBean clienteStkBean = new ClienteStkBean();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            copBean = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);
            copShared.InsertExceptionSTK(exceptionBean, copBean.getId());

            actualizarpagina(exportar);

        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        } catch (Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String AssingKpiFalse() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();
            CopBean copBean = new CopBean();
            ClienteStkBean clienteStkBean = new ClienteStkBean();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            copBean = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);
            copShared.InsertKpisSTK(kpiBean, copBean.getId());

            actualizarpagina(exportar);

        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        } catch (Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String GetDataClientFromSTK() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();
            CopBean copBean = new CopBean();
            ClienteStkBean clienteStkBean = new ClienteStkBean();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            clienteStkBean = copShared.GetAllDataFromClientesSTK(transitionBeanM.getKnkey());

            T_asignStkBean t_asignStkBean = new T_asignStkBean();
            t_asignStkBean = copShared.GetIfOwnerFromSTK(transitionBeanM.getKnkey());

            actualizarpagina(exportar);
        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String GetInputInfoFromCop() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();
            CopBean copBean = new CopBean();
            ClienteStkBean clienteStkBean = new ClienteStkBean();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;


            copBean = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);

//         Boolean exporta1=false;
//         copShared.InsertDefaultValuesEnCop(false, transitionBeanM.getId());
//         
//         Boolean exporta2=true;
            copShared.InsertDefaultValuesEnCop(exportar, transitionBeanM.getId());

            actualizarpagina(exportar);
        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String AssingContactC10True() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();
            CopBean copBean = new CopBean();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            copBean = copShared.GetDataFromCop(transitionBeanM.getId(), true);

            copShared.InsertIntoContats(contactsBean);  //lo q hay desde el jsp

            transitionBeanM.setTime(copShared.GetTheDateTime());

            transitionBeanM.setContacts(true);

//        java.util.Date utilDate = new java.util.Date(); //se convierte de java.util.Date a java.sql.Date
//        utilDate= transitionBeanM.getTime();
//        java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());

            TransitionsBeanImp tbdate2 = new TransitionsBeanImp();
            Date date = new Date();
            DateFormat dateFormatCQL = new SimpleDateFormat("HH:mm:ss dd/MM/yyyy");
            String fecha = dateFormatCQL.format(date);

            transitionBeanM.setContactsDate(fecha);

            copShared.UpdateContactsyContacsDateIntoTransitions(transitionBeanM);

            actualizarpagina(exportar);
        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String DeletePersonAsContac11True() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();
            CopBean copBean = new CopBean();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            copBean = copShared.GetDataFromCop(transitionBeanM.getId(), exportar);

            ContactsBean contactsBeanId = new ContactsBean();
            contactsBeanId = copShared.GetDatosDeContacts(copBean.getId());

            copShared.DeleteContacto(contactsBeanId.getIdcop());

            copShared.ContarContactosEnTransitions(transitionBeanM.getId());

            actualizarpagina(exportar);
        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public String UploadtxtLogAlert06() {
        try {
            UsuarioDto usuario = new UsuarioDto();
            ActionContext contexto = ActionContext.getContext();
            Map<String, Object> sesion = contexto.getSession();
            usuario = (UsuarioDto) sesion.get("usuarioSession");
            Map<String, Object> sesion2 = contexto.getSession();
            transitionBeanM = (TransitionBean) sesion2.get("Knkey");

            TransitionShared transitionShared = new TransitionShared();
            CopShared copShared = new CopShared();
            CopBean copBean = new CopBean();
            ClienteStkBean clienteStkBean = new ClienteStkBean();

            TransitionBean transitionBeanNew = new TransitionBean();
            transitionBeanNew = transitionShared.GetTransitionBeanIdKnkey(transitionBeanM);
            transitionBeanM = transitionBeanNew;

            ConexionFtp conexionFtp = new ConexionFtp(attach);
            File name = getFileNameUpload();
            String n = name.getPath();

            //Cambiar nombre
            String nombree = "";
            String path = "";

            for (int i = 0; i < getNombreArchivo().length(); i++) {
                if (String.valueOf(getNombreArchivo().charAt(i)).equalsIgnoreCase(".")) {
                    nombree += "_" + transitionBeanNew.getKnkey() + String.valueOf(getNombreArchivo().charAt(i));
                } else {
                    nombree += String.valueOf(getNombreArchivo().charAt(i));
                }
            }
            setNombreArchivo(nombree);


            //Cambiar path

            path = name.getParent() + "\\" + getNombreArchivo();
            File name2 = getFileNameUpload();

            //Renombrar nombre de archivo

            renombrar(path, getNombreArchivo());




            setS1("c:\\consultas.rar");
            //File firstLocalFile = new File("c:/consultas.rar");
            File firstLocalFile = new File(n);

            String firstRemoteFile = "consultas.rar";
            String nombre = firstLocalFile.getName();
            FileInputStream inputStream = new FileInputStream(name2);
            //FileInputStream inputStream = new FileInputStream(firstLocalFile);

            System.out.println("Start uploading first file");
            boolean done = conexionFtp.guarda(getNombreArchivo(), "attachments", inputStream);

            fileListOnServer = conexionFtp.enlistaDir(transitionBeanM.getKnkey());

            inputStream.close();
            if (done) {
                System.out.println("The first file is uploaded successfully.");
            }
            actualizarpagina(exportar);
        } catch (CstF2Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        } catch (Exception ex) {
            log.error("ERROR. ", ex);
            addFieldError("KnkeyIncorrecta", "Ocurrio un Error: " + ex.getMessage());
            return ERROR_SESSION;
        }
        if (exportar == false) {
            return SUCCESS;
        } else {
            return EXPORT;
        }
    }

    public static void renombrar(String rutaArchivo, String nuevoNombre) {

        //Creo un objeto File con la ruta dada
        File f1 = new File(rutaArchivo);
        //Evaluo si el archivo existe
        if (f1.exists()) {
            //Creo un objeto 2 para renombrar, tomando la ruta del directorio actual y el nuevo nombre
            //Con el que va a quedar el archivo
            File f2 = new File(f1.getParent() + nuevoNombre);
            //Evaluo si puede ser renombrado el archivo
            if (f1.renameTo(f2)) {
                System.out.println("Archivo " + f1.getAbsolutePath() + " se renombro exitosamente :^D");
            } else {
                System.out.println("Paila fallo el renombrado del archivo " + f1.getAbsolutePath() + " :-$");
            }
        } else {
            System.out.println("Changos! El archivo " + f1.getAbsolutePath() + " no existe :-$");
        }

        //Como pueden notar, aqui no se hizo uso de la excepcion FileNotFoundException, eso se
        //debe a que hago una evaluación previa de existencia con el fin de evitar Excepciones
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
